package com.perye.dokit.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.entity.WeWeddingStyle;
import com.perye.dokit.query.WeWeddingStyleQueryCriteria;
import com.perye.dokit.service.WeWeddingStyleService;
import com.perye.dokit.service.WeddingTypeService;
import com.perye.dokit.utils.ArrayAssembly;
import com.perye.dokit.utils.DateUtils;
import com.perye.dokit.utils.UploadFile;
import com.perye.dokit.utils.pageData.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;


@Api(tags = "styleList管理")
@RestController
@RequestMapping("/api/weWeddingStyle")
public class WeWeddingStyleController extends BaseController {

    @Autowired
    private WeddingTypeService weddingTypeService;
    @Value("${uploadFilePath}")
    String uploadFilePath;

    @Value("${uploadFileMapping}")
    String uploadFileMapping;
    private final WeWeddingStyleService weWeddingStyleService;

    public WeWeddingStyleController(WeWeddingStyleService weWeddingStyleService) {
        this.weWeddingStyleService = weWeddingStyleService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@dokit.check('weWeddingStyle:list')")
    public void download(HttpServletResponse response, WeWeddingStyleQueryCriteria criteria) throws IOException {
        weWeddingStyleService.download(weWeddingStyleService.queryAll(criteria), response);
    }

    @Log("查询styleList")
    @ApiOperation("查询styleList")
    @GetMapping()
    @PreAuthorize("@dokit.check('weWeddingStyle:list')")
    public ResponseEntity<Object> getWeWeddingStyles()throws Exception{
        PageData pageDataList = null;
        try {
            PageData pageData = this.getPageData();
            int page = Integer.parseInt(pageData.getString("page")) + 1;
            PageHelper.startPage(page, Integer.parseInt(pageData.getString("size")));

            List<PageData> deptDtos = weWeddingStyleService.selectWeddingStyle(pageData);


            PageInfo<PageData> pageInfo = new PageInfo<PageData>(deptDtos);

            int count = (int) pageInfo.getTotal();
            pageDataList = ArrayAssembly.pageList(deptDtos, count);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return new ResponseEntity<>(pageDataList, HttpStatus.OK);
    }

    @Log("新增styleList")
    @ApiOperation("新增styleList")
    @PostMapping
    @PreAuthorize("@dokit.check('weWeddingStyle:add')")
    public ResponseEntity<Object> create(@RequestBody PageData pageData)throws Exception{
        WeWeddingStyle resources=new WeWeddingStyle();
        String id= IdUtil.simpleUUID();//风格id
        String coverPath= pageData.get("coverPath").toString();
        String name= pageData.get("name").toString();
        String typeId=pageData.get("typeId").toString();
        String price=pageData.get("price").toString();//价格
        String deposit=pageData.get("deposit").toString();//定金

        String serviceDetails=pageData.get("serviceDetails").toString();//套餐详情
        String priceRecommend=pageData.get("priceRecommend").toString();//是否推荐价格
        String workRecommend=pageData.get("workRecommend").toString();//workRecommend
        String isOpen=pageData.get("isOpen").toString();//是否开启
        resources.setId(id);
        resources.setName(name);
        resources.setTypeId(typeId);
        resources.setPrice(price);
        resources.setDeposit(deposit);
        resources.setServiceDetails(serviceDetails);

        resources.setPriceRecommend(Integer.parseInt(priceRecommend));
        resources.setWorkRecommend(Integer.parseInt(workRecommend));
        resources.setIsOpen(Integer.parseInt(isOpen));
        resources.setIsDelete(0);
        Timestamp time=new Timestamp(System.currentTimeMillis());
        resources.setCreateTime(time);
        resources.setUpdateTime(time);
        weWeddingStyleService.create(resources);
        //更新图片关联表根据id删除图片
        PageData pdImg=new PageData();
        pdImg.put("relationId",id);//风格id
        weWeddingStyleService.delWeVideoPicture(pdImg);
        //然后在插入图片
        PageData pd=new PageData();
        pd.put("id",IdUtil.simpleUUID());
        pd.put("relationId",id);
        pd.put("type",0);//'类型：0：图片；1：视频',
        pd.put("url",coverPath);
        pd.put("sort",0);
        pd.put("isDelete",0);
        pd.put("createTime", DateUtils.getTime());
        pd.put("updateTime", DateUtils.getTime());
        weWeddingStyleService.insterImg(pd);
        return new ResponseEntity<>(weWeddingStyleService.create(resources), HttpStatus.CREATED);
    }

    @Log("修改styleList")
    @ApiOperation("修改styleList")
    @PutMapping
    @PreAuthorize("@dokit.check('weWeddingStyle:edit')")
    public ResponseEntity<Object> update(@RequestBody PageData pageData)throws Exception{
        WeWeddingStyle resources=new WeWeddingStyle();
        String id= pageData.get("id").toString();
        String coverPath= pageData.get("coverPath").toString();
        String name= pageData.get("name").toString();
        String typeId=pageData.get("typeId").toString();
        String price=pageData.get("price").toString();//价格
        String deposit=pageData.get("deposit").toString();//定金

        String serviceDetails=pageData.get("serviceDetails").toString();//套餐详情
        String priceRecommend=pageData.get("priceRecommend").toString();//是否推荐价格
        String workRecommend=pageData.get("workRecommend").toString();//workRecommend
        String isOpen=pageData.get("isOpen").toString();//是否开启
        resources.setId(id);
        resources.setName(name);
        resources.setTypeId(typeId);
        resources.setPrice(price);
        resources.setDeposit(deposit);
        resources.setServiceDetails(serviceDetails);

        resources.setPriceRecommend(Integer.parseInt(priceRecommend));
        resources.setWorkRecommend(Integer.parseInt(workRecommend));
        resources.setIsOpen(Integer.parseInt(isOpen));
        resources.setIsDelete(0);
    weWeddingStyleService.update(resources);
        //更新图片关联表根据id删除图片
        PageData pdImg=new PageData();
        pdImg.put("relationId",id);//风格id
        weWeddingStyleService.delWeVideoPicture(pdImg);
         //然后在插入图片
        PageData pd=new PageData();
        pd.put("id",IdUtil.simpleUUID());
        pd.put("relationId",id);
        pd.put("type",0);//'类型：0：图片；1：视频',
        pd.put("url",coverPath);
        pd.put("sort",0);
        pd.put("isDelete",0);
        pd.put("createTime", DateUtils.getTime());
        pd.put("updateTime", DateUtils.getTime());
        weWeddingStyleService.insterImg(pd);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除styleList")
    @ApiOperation("删除styleList")
    @PreAuthorize("@dokit.check('weWeddingStyle:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) {
    weWeddingStyleService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("上传file")
    @ApiOperation("上传file")
    @RequestMapping("/uploadFile")
    public PageData uploadFile(@RequestParam("file") MultipartFile file) {
        //操作MultipartFile uploadImage, String moduleName
        //返回结果
        PageData result = new PageData();
        //参数  uploadFilePath 上传文件路径//uploadFileMapping 映射路径

        try {
          String filePath=  UploadFile.uploadFile(file,uploadFilePath,uploadFileMapping);
          PageData pd=new PageData();
            String id= IdUtil.simpleUUID();//图片id
            pd.put("id",id);//没啥用
            result.put("code", "00");
            result.put("filePath", filePath);
            result.put("filePathId", id);
            result.put("msg", "上传成功");
        } catch (Exception e) {

        }
        return result;
    }
    @Log("查询类型")
    @ApiOperation("查询类型")
    @RequestMapping("/getTypeList")
    public PageData getTypeList() {
        //返回结果
        PageData pageData = this.getPageData();
        PageData result = new PageData();
        try {
            List<PageData> deptDtos = weddingTypeService.queryWeddingTypeList(pageData);
            result.put("code", "00");
            result.put("typeLists", deptDtos);
        } catch (Exception e) {
        }
        return result;
    }
}
